<?php

namespace Agent\Model;

use Utuo\UtuoModel;

class DistributionShopOrderModel extends UtuoModel {
	
	/* 订单状态 */
	const ORDER_STATUS_INVALID = 0; // 订单异常
	const ORDER_STATUS_CANCELED = 1; // 已取消
	const ORDER_STATUS_PAY = 2;  // 待支付
	const ORDER_STATUS_SHIPPING = 3; // 待发货
	const ORDER_STATUS_SIGN = 4; // 待签收
	const ORDER_STATUS_COMPLETED = 5; // 交易成功
	const ORDER_STATUS_RETURN_APPLY = 6; // 申请退货
	const ORDER_STATUS_RETURN_REFUSED = 7; // 拒绝退货
	const ORDER_STATUS_RETURN_PROCESSING = 8; // 退货中
	const ORDER_STATUS_RETURN_SUCCESS = 9; // 退货成功
	
	/* 付款状态 */
	const PAY_STATUS_NOT_PAID = 0; // 待支付
	const PAY_STATUS_PAID = 1; // 待支付
	
	// 购物状态
	private $status = array(
			0 => '订单异常',
			1 => '已取消',
			2 => '待支付',
			3 => '待发货',
			4 => '待签收',
			5 => '交易成功',
			6 => '申请退货',
			7 => '拒绝退货',
			8 => '退货中',
			9 => '退货成功' 
	);
	
	// 可查询的订单状态列表
	static $TYPES = array(
			1 => "待付款订单",
			2 => "待收货订单",
			3 => "交易成功订单",
			//0 => "其它订单",
	);
	
	/**
	 * 获得订单显示状态
	 */
	public function getOrderShowStatus($status = 0, $payStatus = 1) {
		$statusText = "";
		if(isset($this->status[$status])){
			if(!$payStatus and ($status == self::ORDER_STATUS_SHIPPING or $status == self::ORDER_STATUS_SIGN)) {
				$statusText = $this->status[$status]."(货到付款)";
			}else $statusText =  $this->status[$status];
		} else {
			$statusText = $this->status[0];
		}
		return $statusText;
	}
	
	
	/**
	 * 绑定订单详细
	 * @param unknown $orderLists 查询出的订单列表
	 */
	public function bindOrderDetail(&$orderLists) {
		/* 关联订单商品 */
		$dProductOrder = $this;
		$mProductGoods = M ( "DistributionShopOrderGoods" );
		foreach ( $orderLists as &$v ) {
			// 绑定显示状态
			$v and $v ["orderStatusName"] = $dProductOrder->getOrderShowStatus ( $v ['order_status'], $v ['pay_status'], $v ['shipping_status'] );
			// 绑定订单商品
			$orderGoods = $mProductGoods->where ( "order_id=%d", $v ['id'] )->select ();
			foreach ( $orderGoods as &$v0 ) {
				$v ['orderItems'] [$v0 ['product_id']] or $v ['orderItems'] [$v0 ['product_id']] = array (
						'productId' => $v0 ['product_id'],
						'productName' => $v0 ['product_name'],
						'items' => array ()
				);
				$v ['orderItems'] [$v0 ['product_id']] ['items'] [] = array (
						'orderGoodsId' => $v0 ['id'],
						'productPic' => $v0 ['product_pic'],
						'productNumber' => $v0 ['product_number'],
						'productPrice' => $v0 ['product_price'],
						'productAttr' => $v0 ['product_attr'],
						'rate_status' => $v0 ['rate_status'],
				);
			}
		}
	}
	
	/**
	 * 获得一个订单
	 * @param int $id 订单id 
	 * @param int $uid 用户id 可空
	 * @param int $cid 客户id 可空
	 * @return unknown
	 */
	public function getOrder($id, $uid = 0, $cid = 0) {
		$condition = array(
						"orders.id" => $id,
					 );
		$uid and $condition['uid'] = $uid;
		$cid and $condition['cid'] = $cid;
		$order = M ( "DistributionShopOrder orders" )->join ( "__CN_PROVINCE__ province on orders.province = province.code", "LEFT" )->join ( "__CN_CITY__ city on orders.city = city.code", "LEFT" )->join ( "__CN_AREA__ area on orders.area = area.code", "LEFT" )->field ( "orders.*,province.name as provinceName, city.name as cityName, area.name as areaName" )
											->where ($condition)->find () and
		$orders = array (
						&$order
				  ) and $this->bindOrderDetail ( $orders );
		return $order;
	}
}